package com.Algorithm.kmp;

/**
 * 暴力匹配
 * @author MaoLin Wang
 * @date 2019/11/1515:27
 */
public class ViolencenMatch {

    public static void main(String[] args) {
        String str1="王茂霖茂霖啊啊茂霖";
        String str2="啊啊茂霖";
        int i = violenceMatch(str1, str2);
        System.out.println(i);
    }

    public static int violenceMatch(String str1,String str2){
        char[] chars1 = str1.toCharArray();
        char[] chars2 = str2.toCharArray();

        int str1Len=chars1.length;
        int str2Len=chars2.length;

        int i=0;
        int j=0;
        while (i<str1Len && j< str2Len){
            if (chars1[i] == chars2[j]){
                i++;j++;
            }else {
                i=i-j+1;
                j=0;
            }
        }
        if (j==str2Len){
            return i-j;
        }else {
            return -1;
        }
    }
}
